﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StudiesUiProxyService.cs" company="ELEN 7045 GROUP II">>
//   Contributed by E.Vosloo
// </copyright>
// <summary>
//   The Studies UI Proxy Service Implementation
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Presentation.Module.StudyDefinition.Services
{
    #region

    using System.Collections.ObjectModel;

    using Application.Interfaces;

    using Presentation.Module.StudyDefinition.Interfaces.Services;
    using Presentation.Module.StudyDefinition.Models;
    using Presentation.Module.StudyDefinition.Services.Mappers;

    #endregion

    /// <summary>
    /// The Studies Service Implementation
    /// </summary>
    public class StudiesUiProxyService : IStudiesUIProxyService
    {
        #region Constants and Fields

        /// <summary>
        /// Study Definition Management Service Instance
        /// </summary>
        private readonly IStudyDefinitionManagementService studyDefinitionManagementServiceInstance;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StudiesUiProxyService"/> class.
        /// </summary>
        /// <param name="studyDefinitionManagementService">
        /// The study definition management service.
        /// </param>
        public StudiesUiProxyService(IStudyDefinitionManagementService studyDefinitionManagementService)
        {
            this.studyDefinitionManagementServiceInstance = studyDefinitionManagementService;
        }

        #endregion

        #region Implemented Interfaces

        #region IStudiesService

        /// <summary>
        /// Adds the specified study.
        /// </summary>
        /// <param name="study">
        /// The study.
        /// </param>
        /// <returns>
        /// The add study.
        /// </returns>
        public Study AddStudy(Study study)
        {
            var item = this.studyDefinitionManagementServiceInstance.AddStudy(StudyDefinitionMapper.ModelToEntity(study));
            return StudyDefinitionMapper.EntityToModel(item);
        }

        /// <summary>
        /// Adds the specified subject group.
        /// </summary>
        /// <param name="subjectGroup">
        /// The subject group.
        /// </param>
        /// <returns>
        /// The added subject group.
        /// </returns>
        public SubjectGroup AddSubjectGroup(SubjectGroup subjectGroup)
        {
            var response = this.studyDefinitionManagementServiceInstance.AddSubjectGroup(StudyDefinitionMapper.ModelToEntity(subjectGroup));
            return StudyDefinitionMapper.EntityToModel(response);
        }

        /// <summary>
        /// Adds the specified treatment.
        /// </summary>
        /// <param name="treatment">
        /// The treatment.
        /// </param>
        /// <returns>
        /// The added treatment.
        /// </returns>
        public Treatment AddTreatment(Treatment treatment)
        {
            var item = this.studyDefinitionManagementServiceInstance.AddTreatment(StudyDefinitionMapper.ModelToEntity(treatment));

            return StudyDefinitionMapper.EntityToModel(item);
        }

        /// <summary>
        /// Edits the specified study.
        /// </summary>
        /// <param name="study">
        /// The study.
        /// </param>
        public void EditStudy(Study study)
        {
            this.studyDefinitionManagementServiceInstance.UpdateStudy(StudyDefinitionMapper.ModelToEntity(study));
        }

        /// <summary>
        /// Edits the specified subject group.
        /// </summary>
        /// <param name="subjectGroup">
        /// The subject group.
        /// </param>
        public void EditSubjectGroup(SubjectGroup subjectGroup)
        {
            this.studyDefinitionManagementServiceInstance.UpdateSubjectGroup(StudyDefinitionMapper.ModelToEntity(subjectGroup));
        }

        /// <summary>
        /// Edits the specified treatment.
        /// </summary>
        /// <param name="treatment">
        /// The treatment.
        /// </param>
        public void EditTreatment(Treatment treatment)
        {
            this.studyDefinitionManagementServiceInstance.UpdateTreatment(StudyDefinitionMapper.ModelToEntity(treatment));
        }

        /// <summary>
        /// Gets all studies.
        /// </summary>
        /// <returns>
        /// Study Collection
        /// </returns>
        public ObservableCollection<Study> GetAllStudies()
        {
            var items = this.studyDefinitionManagementServiceInstance.GetAllStudies();

            ObservableCollection<Study> results = new ObservableCollection<Study>();
            foreach (var item in items)
            {
                results.Add(StudyDefinitionMapper.EntityToModel(item));
            }

            return results;
        }

        /// <summary>
        /// Gets all subject groups.
        /// </summary>
        /// <returns>
        /// Subject Group Collection
        /// </returns>
        public ObservableCollection<SubjectGroup> GetAllSubjectGroups()
        {
            var items = this.studyDefinitionManagementServiceInstance.GetAllSubjectGroups();

            ObservableCollection<SubjectGroup> results = new ObservableCollection<SubjectGroup>();
            foreach (var item in items)
            {
                results.Add(StudyDefinitionMapper.EntityToModel(item));
            }

            return results;
        }

        /// <summary>
        /// Gets all treatments.
        /// </summary>
        /// <returns>
        /// Treatment Collection
        /// </returns>
        public ObservableCollection<Treatment> GetAllTreatments()
        {
            ObservableCollection<Treatment> result = new ObservableCollection<Treatment>();

            foreach (var treatment in this.studyDefinitionManagementServiceInstance.GetAllTreatment())
            {
                result.Add(StudyDefinitionMapper.EntityToModel(treatment));
            }

            return result;
        }

        /// <summary>
        /// Removes the specified study.
        /// </summary>
        /// <param name="study">
        /// The study.
        /// </param>
        public void RemoveStudy(Study study)
        {
            this.studyDefinitionManagementServiceInstance.DeleteStudy(StudyDefinitionMapper.ModelToEntity(study));
        }

        /// <summary>
        /// Removes the specified subject group.
        /// </summary>
        /// <param name="subjectGroup">
        /// The subject group.
        /// </param>
        public void RemoveSubjectGroup(SubjectGroup subjectGroup)
        {
            this.studyDefinitionManagementServiceInstance.DeleteSubjectGroup(StudyDefinitionMapper.ModelToEntity(subjectGroup));
        }

        /// <summary>
        /// Removes the specified treatment.
        /// </summary>
        /// <param name="treatment">
        /// The treatment.
        /// </param>
        public void RemoveTreatment(Treatment treatment)
        {
            this.studyDefinitionManagementServiceInstance.DeleteTreatment(StudyDefinitionMapper.ModelToEntity(treatment));
        }

        #endregion
        #endregion
    }
}
